import { ITabExpPanelStore } from '@/core/interface/widgets/store';
import { CtrlController } from './ctrl-controller';
import {
	TabExpPanelActionType,
	ITabExpPanelAbility,
	ITabExpPanelController,
	ITabExpPanelControllerParams,
	ITabExpPanelModel,
	ITabViewPanelAbility,
	IParam,
	ICtrlActionResult,
} from '../../interface';
import { deepCopy } from '@/core/utils';

/**
 * 分页导航面板部件控制器
 *
 * @export
 * @class TabExpPanelController
 * @extends {CtrlController<ITabExpPanelStore>}
 * @implements {ITabExpPanelController}
 * @template T
 */
export class TabExpPanelController
	extends CtrlController<
		TabExpPanelActionType,
		ITabExpPanelStore,
		ITabExpPanelAbility
	>
	implements ITabExpPanelController<ITabExpPanelStore, ITabExpPanelAbility>
{
	/**
	 * Creates an instance of TabExpPanelController.
	 * @param {ITabExpPanelControllerParams<TabExpPanelActionType, ITabExpPanelAbility>} params
	 * @memberof TabExpPanelController
	 */
	public constructor(
		params: ITabExpPanelControllerParams<
			TabExpPanelActionType,
			ITabExpPanelAbility
		>
	) {
		super(params);
		this.ctrlInit(params);
	}

	/**
	 * 处理部件初始化
	 *
	 * @protected
	 * @param {ITabExpPanelControllerParams<TabExpPanelActionType, ITabExpPanelAbility>} params
	 * @memberof TabExpPanelController
	 */
	protected processCtrlInit(
		params: ITabExpPanelControllerParams<
			TabExpPanelActionType,
			ITabExpPanelAbility
		>
	) {
		super.processCtrlInit(params);
		const model = this.getModel<ITabExpPanelModel>();
		Object.assign(this.store, { activeKey: '', tabs: model.tabs });
	}

	/**
	 * 部件挂载
	 *
	 * @memberof TabExpPanelController
	 */
	public ctrlMounted(): void {
		super.ctrlMounted();
		this.handleDefaultActive();
	}

	/**
	 * @description 处理默认激活
	 *
	 * @memberof TabExpPanelController
	 */
	public handleDefaultActive() {
		if (
			this.store.viewParams &&
			(this.store.viewParams.srftabactivate || this.store.viewParams.srfnavtag)
		) {
			this.store.activeKey =
				this.store.viewParams.srftabactivate?.toLowerCase() ||
				this.store.viewParams.srfnavtag?.toLowerCase();
		} else {
			const item = this.store.tabs.find((tab: IParam) => !tab.hidden);
			if (item) {
				this.store.activeKey = item.name;
			}
		}
		Object.assign(this.store.viewParams, {
			srftabactivate: this.store.activeKey,
		});
	}

	/**
	 * 获取数据
	 *
	 * @return {*}  {IParam[]}
	 * @memberof AppMenuController
	 */
	public getData(): IParam[] {
		const data: Array<IParam> = this.store.tabs;
		return data;
	}

	/**
	 * 获取分页视图面板能力
	 *
	 * @return {*}  {(ITabViewPanelAbility | undefined)}
	 * @memberof PickupViewPanelController
	 */
	public getTabViewPanelAbility(): ITabViewPanelAbility | undefined {
		if (this.store.activeKey) {
			return this.getSubAbility<ITabViewPanelAbility>(this.store.activeKey);
		}
		return undefined;
	}

	/**
	 * @description 加载当前分页视图
	 * @memberof TabExpPanelController
	 */
	public load(args?: IParam): Promise<ICtrlActionResult> {
		this.computedPanelResource();
		const { viewParams } = this.store;
		const tempViewParams = Object.assign(deepCopy(viewParams), args);
		const tabViewPanelAbility = this.getTabViewPanelAbility();
		if (tabViewPanelAbility) {
			tabViewPanelAbility.load(tempViewParams);
			return Promise.resolve({
				ok: true,
				data: this.getData(),
				rowData: this.model.tabs,
			});
		}
		return Promise.resolve({
			ok: false,
			data: this.getData(),
			rowData: this.model.tabs,
		});
	}

	/**
	 * @description 获取能力
	 * @return {*}  {ITabExpPanelAbility}
	 * @memberof TabExpPanelController
	 */
	public getAbility(): ITabExpPanelAbility {
		return {
			...super.getAbility(),
			load: this.load.bind(this),
		};
	}

	/**
	 * @description 计算面板权限
	 * @memberof TabExpPanelController
	 */
	public computedPanelResource() {
		if (App.getEnableAppPermission()) {
			const appName = App.getAppConfig().appName;
			const sysName = App.getAppConfig().sysName;
			this.store.tabs.forEach((tab: IParam) => {
				const tag: string = `${sysName}-${appName}-${tab.embedViewCodeName}`;
				if (tag && !App.getAppUniresAuth(tag)) {
					tab.hidden = true;
				}
			});
		}
	}

	/**
	 * @description 处理导航面板点击
	 * @memberof TabExpPanelController
	 */
	public handleTabPanelClick(event: any) {
		if (!event || Object.is(this.store.activeKey, event)) {
			return;
		}
		this.store.activeKey = event;
		Object.assign(this.store.viewParams, {
			srftabactivate: this.store.activeKey,
		});
		this.emit('tabClick', [event]);
	}
}
